package com.lovi.net_.tcp;

import javax.sound.sampled.Line;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

/**
 * 字符流方式
 */
public class SocketTCPClient03 {
    public static void main(String[] args) throws IOException {
        //IP地址,测试为本机ip地址
        InetAddress localHost = InetAddress.getLocalHost();
        Socket socket = new Socket(localHost, 9999);
        System.out.println("客户端socket就绪！");
        //准备发消息给服务器端！！
        OutputStream outputStream = socket.getOutputStream();
        //通过转换流转成字符流,这里转换成writer
        OutputStreamWriter osw = new OutputStreamWriter(outputStream);
        BufferedWriter writer = new BufferedWriter(osw);
        writer.write("客户端：你好呀服务端！！");
        writer.newLine();//相当于结束标志
        writer.flush();

        //客户端需要接受服务器端发送的消息
        InputStream inputStream = socket.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String s = reader.readLine();
        System.out.println(s);
        writer.close();
        osw.close();
        inputStream.close();
        outputStream.close();
        socket.close();
        System.out.println("客户端退出！");
    }
}
